
from urllib.parse import urlencode
from requests.exceptions import RequestException
from hashlib import md5
import requests
import json
import os

def get_page_index(pageNum, keyword):
  url = 'https://www.toutiao.com/'
  query = {
    "index": pageNum,
    "keyword": keyword
  }
  try:
    resp = requests.get(url + '?' + urlencode(query))
    if resp.status_code == 200:
      return resp.text
  except  RequestException:
    print('请求失败')
    return None

# 返回一个迭代器
def parse_json(str):
  data = json.loads(str)
  if data and 'data' in data.keys():
    for item in data.get('data'):
      yield item.get('aarticle_url')

def saveImage(content):
  filePath = '{0}/{1}.{2}'.format(os.getcwd(), md5(content).hexdigest(), 'jpg')
  if not os.path.exists(filePath):
    with open(filePath, 'wb') as f:
      f.write(content)
      f.close()

if __name__ == '__main__':
  get_page_index(0, '街拍')
